/* 
 * Copyright (c) 2004-2005 Massachusetts Institute of Technology. This code was
 * developed as part of the Haystack (http://haystack.lcs.mit.edu/) research 
 * project at MIT. Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including without 
 * limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions: 
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 */
package edu.mit.csail.relo.jdt.parts;

import java.util.ArrayList;
import java.util.List;

import jiggle.Edge;
import jiggle.Graph;
import jiggle.Vertex;
import jiggle.relo.PointedEdge;

import org.eclipse.draw2d.AbsoluteBendpoint;
import org.eclipse.draw2d.BendpointConnectionRouter;
import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.graphics.Color;

import edu.mit.csail.relo.eclipse.gef.RoundedPolylineConnection;
import edu.mit.csail.relo.eclipse.gef.Triangle2;
import edu.mit.csail.relo.graph.GraphAnimation;
import edu.mit.csail.relo.parts.AbstractReloEditPart;
import edu.mit.csail.relo.parts.ArtifactRelEditPart;
import edu.mit.csail.relo.parts.SideCenteredAnchor;


/**
 */
public class InheritanceRelationPart extends ArtifactRelEditPart {
    
    class InheritanceConnectionRouter extends BendpointConnectionRouter {
        private int distFromTgt;
        public InheritanceConnectionRouter (int distFromTgt) {
            this.distFromTgt = distFromTgt;
        }
		@Override
        public void route(Connection conn) {
			GraphAnimation.recordInitialState(conn);
			if (!GraphAnimation.playbackState(conn)) {
	        	List<Object> bendpoints = new ArrayList<Object> (2);
	        	
	        	Point startPoint = getStartPoint(conn);
	        	conn.translateToRelative(startPoint);

	        	Point endPoint = getEndPoint(conn);
	        	conn.translateToRelative(endPoint);

	        	bendpoints.add(new AbsoluteBendpoint(startPoint.x,endPoint.y+distFromTgt));
	        	bendpoints.add(new AbsoluteBendpoint(endPoint.x,endPoint.y+distFromTgt));
	        	
	        	setConstraint(conn, bendpoints);
	            super.route(conn);
			}
		}
    }


    @Override
    protected Edge getEdgeForGraph(Graph graph, Vertex source, Vertex target) {
        //return new PointedEdge(graph, target, source, /*vert*/true, /*asc*/true);
        return new PointedEdge(graph, source, target, /*vert*/true, /*asc*/false);
    }


	@Override
    protected IFigure createFigure() {
		//System.err.println("== == InheritanceRelationPart.createFigure called");

		//PolylineConnection conn = new PolylineConnection();
		RoundedPolylineConnection conn = new RoundedPolylineConnection();
		//conn.setConnectionRouter(new ManhattanConnectionRouter());
		conn.setConnectionRouter(new InheritanceConnectionRouter(15));
		
		//conn.setConnectionRouter(new ManhattanConnectionRouter() {
		//	public void route(Connection conn) {
		//		GraphAnimation.recordInitialState(conn);
		//		if (!GraphAnimation.playbackState(conn))
		//			super.route(conn);
		//	}
		//});

		PolygonDecoration dec = new PolygonDecoration();
		dec.setScale(7, 5);
		dec.setBackgroundColor(new Color(null, 255, 255, 255));
		conn.setTargetDecoration(dec);
		
		return conn;
	}

	@Override
    protected ConnectionAnchor getSourceConnectionAnchor(AbstractReloEditPart srcEP) {
        return new SideCenteredAnchor(srcEP.getFigure(), SideCenteredAnchor.top);
	}
	@Override
    protected ConnectionAnchor getTargetConnectionAnchor(AbstractReloEditPart tgtEP) {
        return new SideCenteredAnchor(tgtEP.getFigure(), SideCenteredAnchor.bottom);
	}


    /* (non-Javadoc)
     * @see edu.mit.csail.relo.parts.ArtifactNavAidsEditPolicy.RelationFigure#getArrow()
     */
    @Override
    public IFigure getArrow() {
        Triangle2 arrowHead = new Triangle2();
        arrowHead.setFill(false);
        return getArrow(PositionConstants.NORTH, arrowHead);
    }

}